<template>
	<view class="gift-purchase">
		<!--pages/purchase/purchase.wxml-->
		<view class="line"></view>
		<view class="viewa">
			<image :src="cover[faceIndex].img_url" mode="widthFix"></image>
		</view>
		<view class="line"></view>
		<view class="viewc">选择卡面</view>
		<scroll-view class="viewb viewb-width" scroll-x>
			<view :class="'viewbInner ' + (faceIndex==index?'viewbInnerck':'')" v-for="(item, index) in cover"
				:key="index" @tap="chooseface" :data-index="index">
				<image :src="item.img_url"></image>
			</view>
		</scroll-view>
		<view class="line"></view>
		<view class="viewc">选择礼品</view>
		<view class="viewd">
			<view v-for="(item, index) in gift" :key="index" @tap.stop="preview" :data-index="index"
				:class="'viewdInner ' + (indexXX==index?'viewdInnerck':'')">
				<view>
					<view class="viewdInnera" @tap.stop="detailTap" :data-index="index">
						{{item.money+'元'+card.title+'礼品卡'}}>
					</view>
					<view class="viewdInnerb">{{item.price}}元</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="left">
				<view class="left2">￥<text>{{total}}</text>
				</view>
			</view>
			<view class="right" @tap="toBuy">购买</view>
		</view>

		<view class="mask" v-if="mask">
			<view class="maskInner">
				<view class="maskInnera">
					<image @tap="close" src="/static/images/close.png"></image>
				</view>
				<view class="maskInnerb">
					<view class="maskInnerb1">{{masktitle}}</view>
					<view class="maskInnerb2">{{maskprice}}元</view>
				</view>
				<view class="maskInnerc">
					<view class="maskInnerc1">剩余数量：<text>{{maskNumber}}</text>
					</view>
					<view class="maskInnerc1">可用时段：<text class="color2">{{card.start_time+' -- '+card.end_time}}</text>
					</view>
					<view class="maskInnerc2">使用须知：
						<view class='maskInnerc2a'></view>
						<view class='maskInnerc2b'>{{card.notes}}</view>
						<view class="maskInnerc2-bottom">
							<block v-for="(item, index) in card.notes" :key="index">
								<view class="maskInnerc2b">{{item}}</view>
							</block>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/purchase/purchase.js
	// var app = getApp().globalData;

	export default {
		data() {
			return {
				faceIndex: 0,
				gift: [],
				masktitle: '',
				maskprice: 0,
				maskTime: '',
				maskPhone: '',
				maskDesc: '',
				sum: 0,
				//总数
				total: 0,
				//总价
				indexXX: -1,
				mask: false,
				maskNumber: "",
				canClick: false,
				id: "",
				cover: "",
				card: "",
				store_id: '',
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		async onLoad(options) {
			await this.$onLaunched;
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			if (options && (options.store_id)) {
				this.$store.store_id = options.store_id;
				this.store_id = options.store_id;
			}

			var that = this;
			this.setData({
			}); //礼品卡详情

			getApp().globalData.sendRequest({
				url: '/GiftCard/getGiftDetail',
				type: "post",
				data: {
				},
				success: function(res) {
					if (res.status == 1) {
						that.setData({
							gift: res.price,
							cover: res.cover,
							card: res.card
						});
					} else {
						that.setData({
							gift: null
						});
					}
				}
			});
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage(res) {
			return {
				title: '购买礼品卡',
				path: '/pages/mine/gift_purchase/gift_purchase?store_id=' + this.$store.store_id +
					'&id=' + this.id + '&share_uid=' + app.XcxData.userInfo.uid,
				success: function(res) { // 转发成功
				},
				fail: function(res) { // 转发失败
				}
			};
		},
		methods: {
			// 选择卡面
			chooseface: function(e) {
				var that = this;
				var index = e.currentTarget.dataset.index;
				that.setData({
					faceIndex: index
				});
			},
			// 礼品详情
			preview: function(e) {
				var that = this;
				var index = e.currentTarget.dataset.index;
				var gift = that.gift; // var total = that.data.total;

				that.setData({
					indexXX: index,
					total: gift[index].price
				});
			},
			detailTap: function(e) {
				var index = e.currentTarget.dataset.index;
				var gift = this.gift;
				var card = this.card;
				console.log(gift[index].money + card.title);
				this.setData({
					mask: true,
					masktitle: gift[index].money + card.title,
					maskprice: gift[index].price,
					maskNumber: gift[index].less_num,
					indexXX: index,
					total: gift[index].price
				});
			},
			// 关闭
			close: function() {
				this.setData({
					mask: false
				});
			},
			toBuy: function() {
				var faceIndex = this.faceIndex;
				var indexXX = this.indexXX;
				var gift = this.gift;
				var cover = this.cover;
				var that = this;

				getApp().globalData.verifyLogin('/pages/mine/gift_purchase/gift_purchase?id=' + that.id +
					'&store_id=' + that
					.store_id, () => {
						uni.showLoading({
							title: '下单中...',
							// #ifndef MP-ALIPAY
							mask: true,
							// #endif
						});
						if (faceIndex >= 0 && indexXX >= 0) {
							getApp().globalData.sendRequest({
								url: '/GiftCard/createOrder',
								type: "post",
								data: {
									id: that.id,
									cover_id: cover[faceIndex].id,
									price_id: gift[indexXX].id
								},
								success: function(ret) {
									if (ret.status == 1) {
										// #ifdef MP-WEIXIN
										uni.requestPayment({
											timeStamp: ret.payData.timeStamp,
											nonceStr: ret.payData.nonceStr,
											//字符串随机数
											package: ret.payData.package,
											signType: ret.payData.signType,
											paySign: ret.payData.paySign,
											success: function(re) {
												if (re.errMsg == 'requestPayment:ok') {
													// 下单
													uni.showLoading({
														title: '查询中...',
														mask: true
													});
													var count = 0;
													that.searchOrder(ret.order_id,
														0); //查询订单
												} else {
													that.setData({
														canClick: true
													});
												}
											}
										});

										// #endif
										// #ifdef MP-ALIPAY
										my.tradePay({
											tradeNO: ret.payData.trade_no,
											success: function(re) {
												if (re.errMsg == 'requestPayment:ok') {
													// 下单
													uni.showLoading({
														title: '查询中...',
														mask: true
													});
													var count = 0;
													that.searchOrder(ret.order_id,
														0); //查询订单
												} else {
													that.setData({
														canClick: true
													});
												}
											}
										});
										// #endif
									} else {
										uni.hideLoading();
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 1500,
											mask: true
										});
									}
								},
								fail: function() {
									uni.hideLoading();
								}
							});
						} else {
							uni.hideLoading();
							uni.showToast({
								title: "请选择礼品卡",
								icon: 'none',
								duration: 1500,
								mask: true
							});
						}
					})

			},
			searchOrder: function(order_no, count) {
				var that = this;
				getApp().globalData.sendRequest({
					url: '/GiftCard/searchOrder',
					type: "post",
					showLoading: false,
					data: {
						order_id: order_no
					},
					success: function(res) {
						if (res.status == 1) {
							uni.hideLoading();
							uni.showToast({
								title: res.msg,
								duration: 1000,
								mask: true,
								success: function() {
									uni.navigateTo({
										url: '../gift_card/gift_card?id=' + order_no
									});
								}
							});
						} else if (res.status == 2) {
							count++;

							if (count > 10) {
								that.setData({
									canClick: true
								});
								uni.hideLoading();
								uni.showModal({
									title: '提醒',
									content: res.msg,
									showCancel: false,
									confirmColor: '#ff5c6d'
								});
							} else {
								setTimeout(function() {
									that.searchOrder(order_no, count);
								}, 1000);
							}
						} else {
							that.setData({
								canClick: true
							});
							uni.hideLoading();
							uni.showModal({
								title: '提醒',
								content: res.msg,
								showCancel: false,
								confirmColor: '#ff5c6d'
							});
						}
					}
				});
			}
		}
	};
</script>
<style>
	@import "./gift_purchase.css";

	.gift-purchase {
		width: 100%
	}
</style>
